home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 2: Applications / Linux Cubed Series 2 - Applications.iso / editors / emacs / xemacs / xemacs-1.004 / xemacs-1 / xemacs-19.13 / lwlib / xlwscrollbar.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-06-08  |  50.7 KB  |  1,983 lines

  1. /* Implements a lightweight scrollbar widget.  
  2.    Copyright (C) 1992, 1993, 1994 Lucid, Inc.
  3.  
  4. This file is part of the Lucid Widget Library.
  5.  
  6. The Lucid Widget Library is free software; you can redistribute it and/or 
  7. modify it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2, or (at your option)
  9. any later version.
  10.  
  11. The Lucid Widget Library is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of 
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with GNU Emacs; see the file COPYING.  If not, write to
  18. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20. /* Created by Douglas Keller <dkeller@vnet.ibm.com> */
  21. /* Last changed 03/24/95 */
  22.  
  23. /*
  24.  * Resources Supported:
  25.  *     XmNforeground
  26.  *     XmNbackground
  27.  *     XmNtopShadowColor
  28.  *     XmNtopShadowPixmap
  29.  *     XmNbottomShadowColor
  30.  *     XmNbottomShadowPixmap
  31.  *     XmNtroughColor
  32.  *     XmNshadowThickness
  33.  *     XmNshowArrows
  34.  *     XmNorientation
  35.  *     XmNborderWidth
  36.  *
  37.  *     XmNminimum
  38.  *     XmNmaximum
  39.  *     XmNvalue
  40.  *     XmNincrement
  41.  *     XmNpageIncrement
  42.  *
  43.  *     XmNvalueChangedCallback
  44.  *     XmNincrementCallback
  45.  *     XmNdecrementCallback
  46.  *     XmNpageIncrementCallback
  47.  *     XmNpageDecrementCallback
  48.  *     XmNtoTopCallback
  49.  *     XmNtoBottomCallback
  50.  *     XmNdragCallback
  51.  *
  52.  *     XmNknobStyle      - values can be: "plain" or "dimple"
  53.  *     XmNarrowPosition  - values can be: "opposite" or "same"
  54.  *
  55.  */
  56.  
  57. #include <stdio.h>
  58. #include <stdlib.h>
  59. #include <limits.h>
  60.  
  61. #include <X11/IntrinsicP.h>
  62. #include <X11/StringDefs.h>
  63. #include <X11/bitmaps/gray>
  64.  
  65. #include "xlwscrollbarP.h"
  66. #include "xlwscrollbar.h"
  67.  
  68. #define DBUG(x)
  69.  
  70. #define MINL(x,y) ((((unsigned long) (x)) < ((unsigned long) (y))) \
  71.            ? ((unsigned long) (x)) : ((unsigned long) (y)))
  72.  
  73. #define VERT(w) (w->sb.orientation == XmVERTICAL)
  74.  
  75. #define SS_MIN 8
  76.  
  77. #define ARROW_UP    0
  78. #define ARROW_DOWN  1
  79. #define ARROW_LEFT  2
  80. #define ARROW_RIGHT 3
  81.  
  82. #define ARM_NONE   0
  83. #define ARM_KNOB   1
  84. #define ARM_UP     2
  85. #define ARM_DOWN   3
  86. #define ARM_PAGEUP 4
  87. #define ARM_PAGEDOWN 5
  88.  
  89. #define BUTTON_NONE         0
  90. #define BUTTON_KNOB         1
  91. #define BUTTON_UP_ARROW     2
  92. #define BUTTON_DOWN_ARROW   3
  93. #define BUTTON_TROUGH_ABOVE 4
  94. #define BUTTON_TROUGH_BELOW 5
  95.  
  96. #define KNOB_PLAIN  0
  97. #define KNOB_DIMPLE 1
  98.  
  99. /************************************************************************
  100. **
  101. ** Resources
  102. **
  103. */
  104. #define offset(field) XtOffset(XlwScrollBarWidget, field)
  105.  
  106. static XtResource resources[] = {
  107.     { XmNforeground, XmCForeground, XtRPixel, sizeof(Pixel),
  108.       offset(sb.foreground), XtRImmediate, XtDefaultForeground },
  109.  
  110.     { XmNtopShadowColor, XmCTopShadowColor, XtRPixel, sizeof(Pixel),
  111.       offset(sb.topShadowColor), XtRImmediate, (XtPointer) ~0 },
  112.     { XmNbottomShadowColor, XmCBottomShadowColor, XtRPixel, sizeof(Pixel),
  113.       offset(sb.bottomShadowColor), XtRImmediate, (XtPointer)~0 },
  114.  
  115.     { XmNtopShadowPixmap, XmCTopShadowPixmap, XtRPixmap, sizeof (Pixmap),
  116.       offset(sb.topShadowPixmap), XtRImmediate, (XtPointer)None},
  117.     { XmNbottomShadowPixmap, XmCBottomShadowPixmap, XtRPixmap, sizeof (Pixmap),
  118.       offset(sb.bottomShadowPixmap), XtRImmediate, (XtPointer)None},
  119.  
  120.     { XmNtroughColor, XmCTroughColor, XtRPixel, sizeof(Pixel),
  121.       offset(sb.troughColor), XtRImmediate, (XtPointer)~0 },
  122.  
  123.     { XmNshadowThickness, XmCShadowThickness, XtRInt, sizeof(int),
  124.       offset(sb.shadowThickness), XtRImmediate, (XtPointer)2 },
  125.  
  126.     { XmNborderWidth, XmCBorderWidth, XtRDimension, sizeof(Dimension),
  127.       offset(core.border_width), XtRImmediate, (XtPointer)0 },
  128.  
  129.     { XmNshowArrows, XmCShowArrows, XtRBoolean, sizeof(Boolean),
  130.       offset(sb.showArrows), XtRImmediate, (XtPointer)True },
  131.  
  132.     { XmNinitialDelay, XmCInitialDelay, XtRInt, sizeof(int),
  133.       offset(sb.initialDelay), XtRImmediate, (XtPointer) 250 },
  134.     { XmNrepeatDelay, XmCRepeatDelay, XtRInt, sizeof(int),
  135.       offset(sb.repeatDelay), XtRImmediate, (XtPointer) 50 },
  136.  
  137.     { XmNorientation, XmCOrientation, XtROrientation, sizeof(unsigned char),
  138.       offset(sb.orientation), XtRImmediate, (XtPointer) XmVERTICAL },
  139.  
  140.  
  141.     { XmNminimum, XmCMinimum, XtRInt, sizeof(int),
  142.       offset(sb.minimum), XtRImmediate, (XtPointer) 0},
  143.     { XmNmaximum, XmCMaximum, XtRInt, sizeof(int),
  144.       offset(sb.maximum), XtRImmediate, (XtPointer) 100},
  145.     { XmNvalue, XmCValue, XtRInt, sizeof(int),
  146.       offset(sb.value), XtRImmediate, (XtPointer) 0},
  147.     { XmNsliderSize, XmCSliderSize, XtRInt, sizeof(int),
  148.       offset(sb.sliderSize), XtRImmediate, (XtPointer) 10},
  149.     { XmNincrement, XmCIncrement, XtRInt, sizeof(int),
  150.       offset(sb.increment), XtRImmediate, (XtPointer) 1},
  151.     { XmNpageIncrement, XmCPageIncrement, XtRInt, sizeof(int),
  152.       offset(sb.pageIncrement), XtRImmediate, (XtPointer) 10},
  153.  
  154.     { XmNvalueChangedCallback, XmCValueChangedCallback, XtRCallback, sizeof(XtPointer),
  155.       offset(sb.valueChangedCBL), XtRCallback, NULL},
  156.     { XmNincrementCallback, XmCIncrementCallback, XtRCallback, sizeof(XtPointer),
  157.       offset(sb.incrementCBL), XtRCallback, NULL},
  158.     { XmNdecrementCallback, XmCDecrementCallback, XtRCallback, sizeof(XtPointer),
  159.       offset(sb.decrementCBL), XtRCallback, NULL},
  160.     { XmNpageIncrementCallback, XmCPageIncrementCallback, XtRCallback, sizeof(XtPointer),
  161.       offset(sb.pageIncrementCBL), XtRCallback, NULL},
  162.     { XmNpageDecrementCallback, XmCPageDecrementCallback, XtRCallback, sizeof(XtPointer),
  163.       offset(sb.pageDecrementCBL), XtRCallback, NULL},
  164.     { XmNtoTopCallback, XmCToTopCallback, XtRCallback, sizeof(XtPointer),
  165.       offset(sb.toTopCBL), XtRCallback, NULL},
  166.     { XmNtoBottomCallback, XmCToBottomCallback, XtRCallback, sizeof(XtPointer),
  167.       offset(sb.toBottomCBL), XtRCallback, NULL},
  168.     { XmNdragCallback, XmCDragCallback, XtRCallback, sizeof(XtPointer),
  169.       offset(sb.dragCBL), XtRCallback, NULL},
  170.  
  171.     { XmNknobStyle, XmCKnobStyle, XtRString, sizeof(char *),
  172.       offset(sb.knobStyle), XtRImmediate, NULL},
  173.  
  174.     { XmNarrowPosition, XmCArrowPosition, XtRString, sizeof(char *),
  175.       offset(sb.arrowPosition), XtRImmediate, NULL},
  176. };
  177.  
  178. /************************************************************************
  179. **
  180. ** Prototypes
  181. **
  182. */
  183.  
  184. /*
  185. ** Actions
  186. */
  187. static void Select(Widget w, XEvent *event, String *parms, Cardinal *num_parms);
  188. static void Drag(Widget w, XEvent *event, String *parms, Cardinal *num_parms);
  189. static void Release(Widget w, XEvent *event, String *parms, Cardinal *num_parms);
  190. static void Jump(Widget w, XEvent *event, String *parms, Cardinal *num_parms);
  191. static void Abort(Widget w, XEvent *event, String *parms, Cardinal *num_parms);
  192.  
  193. /*
  194. ** Methods
  195. */
  196. static void Initialize(Widget treq, Widget tnew, ArgList args, Cardinal *num_args);
  197. static Boolean SetValues(Widget current, Widget request, Widget nw, ArgList args, Cardinal *num_args);
  198. static void Destroy(Widget widget);
  199. static void Redisplay(Widget widget, XEvent *event, Region region);
  200. static void Resize(Widget widget);
  201. static void Realize(Widget widget, XtValueMask *valuemask, XSetWindowAttributes *attr);
  202.  
  203. /*
  204. ** Private
  205. */
  206.  
  207.  
  208. /************************************************************************
  209. **
  210. ** Actions Table
  211. **
  212. */
  213. static XtActionsRec actions[] = {
  214.     {"Select",     Select},
  215.     {"Drag",       Drag},
  216.     {"Release",    Release},
  217.     {"Jump",       Jump},
  218.     {"Abort",      Abort},
  219. };
  220.  
  221. /************************************************************************
  222. **
  223. ** Default Translation Table
  224. **
  225. */
  226. static char default_translations[] =
  227.     "<Btn1Down>:    Select()\n"
  228.     "<Btn1Motion>:  Drag()\n"
  229.     "<Btn1Up>:      Release()\n"
  230.     "<Btn2Down>:    Jump()\n"
  231.     "<Btn2Motion>:  Drag()\n"
  232.     "<Btn2Up>:      Release()\n"
  233.     "<Key>Delete:   Abort()"
  234. ;
  235.  
  236. /************************************************************************
  237. **
  238. ** Class record initalization
  239. **
  240. */
  241. XlwScrollBarClassRec xlwScrollBarClassRec = {
  242.     /* core_class fields */
  243.     {
  244.     /* superclass          */ (WidgetClass) &coreClassRec,
  245.     /* class_name          */ "XlwScrollBar",
  246.     /* widget_size         */ sizeof(XlwScrollBarRec),
  247.     /* class_initialize    */ NULL,
  248.     /* class_part_init     */ NULL,
  249.     /* class_inited        */ False,
  250.     /* initialize          */ Initialize,
  251.     /* initialize_hook     */ NULL,
  252.     /* realize             */ Realize,
  253.     /* actions             */ actions,
  254.     /* num_actions         */ XtNumber(actions),
  255.     /* resources           */ resources,
  256.     /* num_resources       */ XtNumber(resources),
  257.     /* xrm_class           */ NULLQUARK,
  258.     /* compress_motion     */ True,
  259.     /* compress_exposure   */ XtExposeCompressMultiple,
  260.     /* compress_enterleave */ True,
  261.     /* visible_interest    */ False,
  262.     /* destroy             */ Destroy,
  263.     /* resize              */ Resize,
  264.     /* expose              */ Redisplay,
  265.     /* set_values          */ SetValues,
  266.     /* set_values_hook     */ NULL,
  267.     /* set_values_almost   */ XtInheritSetValuesAlmost,
  268.     /* get_values_hook     */ NULL,
  269.     /* accept_focus        */ NULL,
  270.     /* version             */ XtVersionDontCheck,
  271.     /* callback_private    */ NULL,
  272.     /* tm_table            */ default_translations,
  273.     /* query_geometry      */ NULL,
  274.     },
  275.     /* scrollbar_class fields */
  276.     {
  277.         /* dummy_field         */ 0,
  278.     },
  279. };
  280.  
  281. WidgetClass xlwScrollBarWidgetClass = (WidgetClass) &xlwScrollBarClassRec;
  282.  
  283. /************************************************************************
  284. **
  285. ** Debug funcitons
  286. **
  287. */
  288.  
  289. #ifdef SHOW_CLEAR
  290. static void myXClearArea(Display *dpy, Drawable d, int x, int y, int w, int h, Boolean exp, XlwScrollBarWidget widget)
  291. {
  292.   XFillRectangle(dpy, d, widget->sb.topShadowGC, x, y, w, h);
  293.   XSync(dpy, False);
  294.   sleep(2);
  295.   XClearArea(dpy, d, x, y, w, h, exp);
  296. }
  297.  
  298. #define XClearArea(dpy,win,x,y,width,height,exp) myXClearArea(dpy,win,x,y,width,height,exp,w)
  299. #endif
  300.  
  301. #ifdef CHECK_VALUES
  302. static void check(XlwScrollBarWidget w)
  303. {
  304.   int height;
  305.   
  306.   height= widget_h(w);
  307.   if( w->sb.showArrows ) height -= (2*arrow_h(w));
  308.  
  309.   if( (w->sb.above + w->sb.ss + w->sb.below > height) ||
  310.       (w->sb.value < w->sb.minimum) ||
  311.       (w->sb.value > w->sb.maximum - w->sb.sliderSize )
  312.       )
  313.       {
  314.       printf("above=%d ss=%d below=%d height=%d\n", 
  315.          w->sb.above, w->sb.ss, w->sb.below, height);
  316.       printf("value=%d min=%d max=%d ss=%d max-ss=%d\n",
  317.          w->sb.value, w->sb.minimum, w->sb.maximum, w->sb.sliderSize, w->sb.maximum - w->sb.sliderSize);
  318.       abort();
  319.       }
  320. }
  321.  
  322. #  define CHECK(w) check(w)
  323. #else
  324. #  define CHECK(w)
  325. #endif
  326.  
  327. /************************************************************************
  328. **
  329. ** Static funcitons
  330. **
  331. */
  332.  
  333. static void call_callbacks(XlwScrollBarWidget w, int reason, int value, int pixel,
  334.                XEvent *event)
  335. {
  336.   XlwScrollBarCallbackStruct cbs;
  337.   Boolean called_anything;
  338.  
  339.   cbs.reason  = reason;
  340.   cbs.event   = event;
  341.   cbs.value   = value;
  342.   cbs.pixel   = pixel;
  343.  
  344.   called_anything = False;
  345.  
  346.   switch( reason )
  347.       {
  348.       case XmCR_VALUE_CHANGED:
  349.       XtCallCallbackList( (Widget)w, w->sb.valueChangedCBL, &cbs );
  350.       called_anything = True;
  351.       break;
  352.       case XmCR_INCREMENT:
  353.       if( w->sb.incrementCBL )
  354.           {
  355.           XtCallCallbackList( (Widget)w, w->sb.incrementCBL, &cbs );
  356.           called_anything = True;
  357.           }
  358.       break;
  359.       case XmCR_DECREMENT:
  360.       if( w->sb.decrementCBL )
  361.           {
  362.           XtCallCallbackList( (Widget)w, w->sb.decrementCBL, &cbs );
  363.           called_anything = True;
  364.           }
  365.       break;
  366.       case XmCR_PAGE_INCREMENT:
  367.       if( w->sb.incrementCBL )
  368.           {
  369.           XtCallCallbackList( (Widget)w, w->sb.pageIncrementCBL, &cbs );
  370.           called_anything = True;
  371.           }
  372.       break;
  373.       case XmCR_PAGE_DECREMENT:
  374.       if( w->sb.decrementCBL )
  375.           {
  376.           XtCallCallbackList( (Widget)w, w->sb.pageDecrementCBL, &cbs );
  377.           called_anything = True;
  378.           }
  379.       break;
  380.       case XmCR_TO_TOP:
  381.       if( w->sb.toTopCBL )
  382.           {
  383.           XtCallCallbackList( (Widget)w, w->sb.toTopCBL, &cbs );
  384.           called_anything = True;
  385.           }
  386.       break;
  387.       case XmCR_TO_BOTTOM:
  388.       if( w->sb.toBottomCBL )
  389.           {
  390.           XtCallCallbackList( (Widget)w, w->sb.toBottomCBL, &cbs );
  391.           called_anything = True;
  392.           }
  393.       break;
  394.       case XmCR_DRAG:
  395.       if( w->sb.dragCBL )
  396.           {
  397.           XtCallCallbackList( (Widget)w, w->sb.dragCBL, &cbs );
  398.           }
  399.       called_anything = True; /* Special Case */
  400.       break;
  401.       }
  402.  
  403.   if( !called_anything )
  404.       {
  405.       cbs.reason = XmCR_VALUE_CHANGED;
  406.       XtCallCallbackList( (Widget)w, w->sb.valueChangedCBL, &cbs );
  407.       }
  408. }
  409.  
  410. /*
  411. ** Widget sizes minus the shadow and highlight area
  412. **
  413. */
  414. static int widget_x(XlwScrollBarWidget w)
  415. {
  416.   return( w->sb.shadowThickness );
  417. }
  418.  
  419. static int widget_y(XlwScrollBarWidget w)
  420. {
  421.   return( w->sb.shadowThickness );
  422. }
  423.  
  424. static int widget_w(XlwScrollBarWidget w)
  425. {
  426.   int width, x = w->sb.shadowThickness;
  427.  
  428.   width = VERT(w) ? w->core.width : w->core.height;
  429.  
  430.   if( width <= (2 * x) )
  431.       return( 1 );
  432.   else
  433.       return( width - (2 * x) );
  434. }
  435.  
  436. static int widget_h(XlwScrollBarWidget w)
  437. {
  438.   int height, y = w->sb.shadowThickness;
  439.  
  440.   height = VERT(w) ? w->core.height : w->core.width;
  441.  
  442.   if( height <= (2 * y) )
  443.       return( 1 );
  444.   else
  445.       return( height - (2 * y) );
  446. }
  447.  
  448. static int arrow_h(XlwScrollBarWidget w)
  449. {
  450.   int width, height;
  451.  
  452.   width = widget_w(w);
  453.   height= widget_h(w);
  454.  
  455.   if( width > ((height / 2) - (SS_MIN / 2) - 1) )
  456.       {
  457.       return( ((height / 2) - (SS_MIN / 2) - 1) );
  458.       }
  459.   else
  460.       {
  461.       return( width );
  462.       }
  463. }
  464.  
  465. static int event_x(XlwScrollBarWidget w, XEvent *event)
  466. {
  467.   return( VERT(w) ? event->xbutton.x : event->xbutton.y );
  468. }
  469.  
  470. static int event_y(XlwScrollBarWidget w, XEvent *event)
  471. {
  472.   return( VERT(w) ? event->xbutton.y : event->xbutton.x );
  473. }
  474.  
  475. /*
  476. ** Safe addition and subtraction
  477. */
  478. static int safe_add(int a, int b)
  479. {
  480.   if( a > 0 && INT_MAX - a < b ) return( INT_MAX );
  481.   else return( a + b );
  482. }
  483.  
  484. static int safe_subtract(int a, int b)
  485. {
  486.   if( a < 0 && -(INT_MIN - a) < b ) return( INT_MIN );
  487.   else return( a - b );
  488. }
  489.  
  490. static int knob_style(XlwScrollBarWidget w)
  491. {
  492.   if( w->sb.knobStyle )
  493.       {
  494.       if( w->sb.knobStyle[0] == 'd' )
  495.       {
  496.       return( KNOB_DIMPLE );
  497.       }
  498.       }
  499.   return( KNOB_PLAIN );
  500. }
  501.  
  502. static Boolean arrow_same_end(XlwScrollBarWidget w)
  503. {
  504.   if( w->sb.arrowPosition && w->sb.arrowPosition[0] == 's' )
  505.       {
  506.       return( True );
  507.       }
  508.   return( False );
  509. }
  510.  
  511. /*
  512. ** GC and Pixel allocation
  513. */
  514. static GC get_gc(XlwScrollBarWidget w, Pixel fg, Pixel bg, Pixmap pm)
  515. {
  516.   XGCValues values;
  517.   XtGCMask mask;
  518.  
  519.   if (pm == w->sb.grayPixmap) {
  520.       /* If we're using the gray pixmap, guarantee white on black ...
  521.        * otherwise, we could end up with something odd like grey on white
  522.        * when we're on a color display that ran out of color cells
  523.        */
  524.  
  525.       fg = WhitePixelOfScreen(DefaultScreenOfDisplay(XtDisplay(w)));
  526.       bg = BlackPixelOfScreen(DefaultScreenOfDisplay(XtDisplay(w)));
  527.   }
  528.  
  529.   values.foreground = fg;
  530.   values.background = bg;
  531.   values.fill_style = FillOpaqueStippled;
  532.   values.stipple    = pm;
  533.   mask              = GCForeground | GCBackground |
  534.       ( pm == None ? 0 : GCStipple | GCFillStyle );
  535.   return( XtGetGC((Widget)w, mask, &values) );
  536. }
  537.  
  538. static void make_shadow_pixels(XlwScrollBarWidget w)
  539. {
  540.   Display *dpy = XtDisplay((Widget) w);
  541.   Colormap cmap = w->core.colormap;
  542.   XColor topc, botc;
  543.   int top_frobbed, bottom_frobbed;
  544.   Pixel bg, fg;
  545.  
  546.   top_frobbed = bottom_frobbed = 0;
  547.  
  548.   bg = w->core.background_pixel;
  549.   fg = w->sb.foreground;
  550.  
  551.   if( w->sb.topShadowColor    == (Pixel)~0) w->sb.topShadowColor    = bg;
  552.   if( w->sb.bottomShadowColor == (Pixel)~0) w->sb.bottomShadowColor = fg;
  553.  
  554.   if( w->sb.topShadowColor == bg || w->sb.topShadowColor == fg )
  555.       {
  556.       topc.pixel = bg;
  557.       XQueryColor( dpy, cmap, &topc );
  558.       /* don't overflow/wrap! */
  559.       topc.red   = MINL(65535, topc.red   * 1.2);
  560.       topc.green = MINL(65535, topc.green * 1.2);
  561.       topc.blue  = MINL(65535, topc.blue  * 1.2);
  562.       if( XAllocColor(dpy, cmap, &topc) )
  563.       {
  564.       if( topc.pixel == bg )
  565.           {
  566.           XFreeColors( dpy, cmap, &topc.pixel, 1, 0);
  567.           topc.red   = MINL(65535, topc.red   + 0x8000);
  568.           topc.green = MINL(65535, topc.green + 0x8000);
  569.           topc.blue  = MINL(65535, topc.blue  + 0x8000);
  570.           if( XAllocColor(dpy, cmap, &topc) )
  571.           {
  572.           w->sb.topShadowColor = topc.pixel;
  573.           }
  574.           }
  575.       else
  576.           {
  577.           w->sb.topShadowColor = topc.pixel;
  578.           }
  579.  
  580.       top_frobbed = 1;
  581.       }
  582.       }
  583.  
  584.   if( w->sb.bottomShadowColor == fg || w->sb.bottomShadowColor == bg )
  585.       {
  586.       botc.pixel = bg;
  587.       XQueryColor( dpy, cmap, &botc );
  588.       botc.red   *= 0.6;
  589.       botc.green *= 0.6;
  590.       botc.blue  *= 0.6;
  591.       if( XAllocColor(dpy, cmap, &botc) )
  592.       {
  593.       if( botc.pixel == bg )
  594.           {
  595.           XFreeColors( dpy, cmap, &botc.pixel, 1, 0);
  596.           botc.red   = MINL(65535, botc.red   + 0x4000);
  597.           botc.green = MINL(65535, botc.green + 0x4000);
  598.           botc.blue  = MINL(65535, botc.blue  + 0x4000);
  599.           if( XAllocColor(dpy, cmap, &botc) )
  600.           {
  601.           w->sb.bottomShadowColor = botc.pixel;
  602.           }
  603.           }
  604.       else
  605.           {
  606.           w->sb.bottomShadowColor = botc.pixel;
  607.           }
  608.       bottom_frobbed = 1;
  609.       }
  610.       }
  611.  
  612.   if( top_frobbed && bottom_frobbed )
  613.       {
  614.       int top_avg = ((topc.red / 3) + (topc.green / 3) + (topc.blue / 3));
  615.       int bot_avg = ((botc.red / 3) + (botc.green / 3) + (botc.blue / 3));
  616.       if( bot_avg > top_avg )
  617.       {
  618.       Pixel tmp = w->sb.topShadowColor;
  619.       w->sb.topShadowColor = w->sb.bottomShadowColor;
  620.       w->sb.bottomShadowColor = tmp;
  621.       }
  622.       else if( topc.pixel == botc.pixel )
  623.       {
  624.       if( botc.pixel == bg )
  625.           w->sb.topShadowColor = bg;
  626.       else
  627.           w->sb.bottomShadowColor = fg;
  628.       }
  629.       }
  630.  
  631.   if (w->sb.topShadowColor == w->core.background_pixel || 
  632.       w->sb.bottomShadowColor == w->core.background_pixel) {
  633.  
  634.       /* Assume we're in mono. This code should be okay even if we're
  635.        * really in color but just short on color cells -- We want the 
  636.        * following behavior, which has been empirically determined to
  637.        * work well for all fg/bg combinations in mono: If the trough
  638.        * and thumb are BOTH black, then use a white top shadow and a
  639.        * grey bottom shadow, otherwise use a grey top shadow and a
  640.        * black bottom shadow.
  641.        */
  642.  
  643.       Pixel white = WhitePixelOfScreen(DefaultScreenOfDisplay(XtDisplay(w)));
  644.       Pixel black = BlackPixelOfScreen(DefaultScreenOfDisplay(XtDisplay(w)));
  645.  
  646.       /* Note: core.background_pixel is the color of the thumb ... */
  647.  
  648.       if (w->core.background_pixel == black &&
  649.         w->sb.troughColor == black) {
  650.  
  651.         w->sb.topShadowColor = white;
  652.         w->sb.bottomShadowPixmap = w->sb.grayPixmap;
  653.  
  654.       } else {
  655.  
  656.         w->sb.topShadowPixmap = w->sb.grayPixmap;
  657.         w->sb.bottomShadowColor = black;
  658.       }
  659.  
  660.   }
  661.       
  662.  
  663. }
  664.  
  665. static void make_trough_pixel(XlwScrollBarWidget w)
  666. {
  667.   Display *dpy = XtDisplay((Widget) w);
  668.   Colormap cmap = DefaultColormapOfScreen( XtScreen((Widget)w) );
  669.   XColor troughC;
  670.  
  671.   if( w->sb.troughColor == (Pixel)~0) w->sb.troughColor = w->core.background_pixel;
  672.  
  673.   if( w->sb.troughColor == w->core.background_pixel )
  674.       {
  675.       troughC.pixel = w->core.background_pixel;
  676.       XQueryColor( dpy, cmap, &troughC );
  677.       troughC.red   *= 0.8;
  678.       troughC.green *= 0.8;
  679.       troughC.blue  *= 0.8;
  680.       if( XAllocColor(dpy, cmap, &troughC) )
  681.       {
  682.       w->sb.troughColor = troughC.pixel;
  683.       }
  684.       }
  685. }
  686.  
  687. /*
  688. ** Draw 3d border
  689. */
  690. static void draw_shadows(Display *dpy, Drawable d, 
  691.              GC shine_gc, GC shadow_gc, 
  692.              int x, int y, int width, int height, 
  693.              int shadowT)
  694. {
  695.    XSegment shine[10], shadow[10];
  696.    int i;
  697.  
  698.    if(shadowT > (width / 2)) shadowT = (width / 2);
  699.    if(shadowT > (height / 2)) shadowT = (height / 2);
  700.    if(shadowT <= 0) return;
  701.  
  702.    for(i = 0; i < shadowT; i++)
  703.        {
  704.        /*  Top segments  */
  705.        shine[i].x1 = x;
  706.        shine[i].y2 = shine[i].y1 = y + i;
  707.        shine[i].x2 = x + width - i - 1;
  708.        /*  Left segments  */
  709.        shine[i + shadowT].x2 = shine[i + shadowT].x1 = x + i;
  710.        shine[i + shadowT].y1 = y + shadowT;
  711.        shine[i + shadowT].y2 = y + height - i - 1;
  712.  
  713.        /*  Bottom segments  */
  714.        shadow[i].x1 = x + i;
  715.        shadow[i].y2 = shadow[i].y1 = y + height - i - 1;
  716.        shadow[i].x2 = x + width - 1 ;
  717.        /*  Right segments  */
  718.        shadow[i + shadowT].x2 = shadow[i + shadowT].x1 = x + width - i - 1;
  719.        shadow[i + shadowT].y1 = y + i + 1;
  720.        shadow[i + shadowT].y2 = y + height - 1 ;
  721.        }
  722.  
  723.    XDrawSegments( dpy, d, shine_gc, shine, shadowT * 2 );
  724.    XDrawSegments( dpy, d, shadow_gc, shadow, shadowT * 2 );
  725. }
  726.  
  727. /*
  728. ** Draw 3d arrows, left, up, down, and right
  729. */
  730. static int make_vert_seg(XSegment *seg, int x1, int y1, int x2, int y2, int shadowT)
  731. {
  732.   int i;
  733.  
  734.   for(i=0; i<shadowT; i++)
  735.       {
  736.       seg[i].x1 = x1;
  737.       seg[i].y1 = y1 + i;
  738.       seg[i].x2 = x2;
  739.       seg[i].y2 = y2 + i;
  740.       }
  741.   return( shadowT );
  742. }
  743.  
  744. static int make_hor_seg(XSegment *seg, int x1, int y1, int x2, int y2, int shadowT)
  745. {
  746.   int i;
  747.  
  748.   for(i=0; i<shadowT; i++)
  749.       {
  750.       seg[i].x1 = x1 + i;
  751.       seg[i].y1 = y1;
  752.       seg[i].x2 = x2 + i;
  753.       seg[i].y2 = y2;
  754.       }
  755.   return( shadowT );
  756. }
  757.  
  758. static void draw_arrow_up(Display *dpy, Drawable win, GC bgGC, GC shineGC, GC shadowGC,
  759.               int x, int y, int width, int height, int shadowT)
  760. {
  761.   XSegment shine[10], shadow[10];
  762.   XPoint triangle[3];
  763.   int mid;
  764.  
  765.   mid = width / 2;
  766.  
  767.   if(shadowT > (width / 2)) shadowT = (width / 2);
  768.   if(shadowT > (height / 2)) shadowT = (height / 2);
  769.   if(shadowT <= 0) shadowT = 0;
  770.  
  771.   /*  /  */
  772.   make_vert_seg( shine,
  773.          x,       y + height - shadowT - 1,
  774.          x + mid, y, shadowT );
  775.   /*  _\  */
  776.   make_vert_seg( shadow,
  777.          x,             y + height - shadowT - 1,
  778.          x + width - 1, y + height - shadowT - 1, shadowT );
  779.   make_vert_seg( shadow + shadowT,
  780.          x + mid,       y,
  781.          x + width - 1, y + height - shadowT - 1, shadowT );
  782.  
  783.   triangle[0].x = x;
  784.   triangle[0].y = y + height - 1;
  785.   triangle[1].x = x + mid;
  786.   triangle[1].y = y;
  787.   triangle[2].x = x + width - 1;
  788.   triangle[2].y = y + height - 1;
  789.  
  790.   XFillPolygon( dpy, win, bgGC, triangle, 3, Convex, ArcChord );
  791.  
  792.   XDrawSegments( dpy, win, shadowGC, shadow, shadowT * 2 );
  793.   XDrawSegments( dpy, win, shineGC, shine, shadowT );
  794. }
  795.  
  796. static void draw_arrow_left(Display *dpy, Drawable win, GC bgGC, GC shineGC, GC shadowGC,
  797.                 int x, int y, int width, int height, int shadowT)
  798. {
  799.   XSegment shine[10], shadow[10];
  800.   XPoint triangle[3];
  801.   int mid;
  802.  
  803.   mid = width / 2;
  804.  
  805.   if(shadowT > (width / 2)) shadowT = (width / 2);
  806.   if(shadowT > (height / 2)) shadowT = (height / 2);
  807.   if(shadowT <= 0) shadowT = 0;
  808.  
  809.   /*  /  */
  810.   make_hor_seg( shine,
  811.          x,         y + mid,
  812.          x + width - shadowT - 1, y, shadowT );
  813.   /*  \|  */
  814.   make_hor_seg( shadow,
  815.          x,         y + mid,
  816.          x + width - shadowT - 1, y + height - 1, shadowT );
  817.   make_hor_seg( shadow + shadowT,
  818.          x + width - shadowT - 1, y,
  819.          x + width - shadowT - 1, y + height - 1, shadowT );
  820.  
  821.   triangle[0].x = x + width - 1;
  822.   triangle[0].y = y + height - 1;
  823.   triangle[1].x = x;
  824.   triangle[1].y = y + mid;
  825.   triangle[2].x = x + width - 1;
  826.   triangle[2].y = y;
  827.  
  828.   XFillPolygon( dpy, win, bgGC, triangle, 3, Convex, ArcChord );
  829.  
  830.   XDrawSegments( dpy, win, shadowGC, shadow, shadowT * 2 );
  831.   XDrawSegments( dpy, win, shineGC, shine, shadowT );
  832. }
  833.  
  834. static void draw_arrow_down(Display *dpy, Drawable win, GC bgGC, GC shineGC, GC shadowGC,
  835.                 int x, int y, int width, int height, int shadowT)
  836. {
  837.   XSegment shine[10], shadow[10];
  838.   XPoint triangle[3];
  839.   int mid;
  840.  
  841.   mid = width / 2;
  842.  
  843.   if(shadowT > (width / 2)) shadowT = (width / 2);
  844.   if(shadowT > (height / 2)) shadowT = (height / 2);
  845.   if(shadowT <= 0) shadowT = 0;
  846.       
  847.   /*  \-  */
  848.   make_vert_seg( shine,
  849.          x,       y,
  850.          x + mid, y + height - shadowT - 1, shadowT );
  851.   make_vert_seg( shine + shadowT,
  852.          x,             y,
  853.          x + width - 1, y, shadowT );
  854.   /*  /  */
  855.   make_vert_seg( shadow,
  856.          x + width - 1, y,
  857.          x + mid,       y + height - shadowT - 1, shadowT );
  858.  
  859.   triangle[0].x = x;
  860.   triangle[0].y = y;
  861.   triangle[1].x = x + mid;
  862.   triangle[1].y = y + height - 1;
  863.   triangle[2].x = x + width - 1;
  864.   triangle[2].y = y;
  865.  
  866.   XFillPolygon( dpy, win, bgGC, triangle, 3, Convex, ArcChord );
  867.  
  868.   XDrawSegments( dpy, win, shadowGC, shadow, shadowT );
  869.   XDrawSegments( dpy, win, shineGC, shine, shadowT * 2 );
  870. }
  871.  
  872. static void draw_arrow_right(Display *dpy, Drawable win, GC bgGC, GC shineGC, GC shadowGC,
  873.                  int x, int y, int width, int height, int shadowT)
  874. {
  875.   XSegment shine[10], shadow[10];
  876.   XPoint triangle[3];
  877.   int mid;
  878.  
  879.   mid = width / 2;
  880.  
  881.   if(shadowT > (width / 2)) shadowT = (width / 2);
  882.   if(shadowT > (height / 2)) shadowT = (height / 2);
  883.   if(shadowT <= 0) shadowT = 0;
  884.       
  885.   /*  |\  */
  886.   make_hor_seg( shine,
  887.         x,       y,
  888.         x + width - shadowT - 1, y + mid, shadowT );
  889.   make_hor_seg( shine + shadowT,
  890.         x, y,
  891.         x, y + height -1, shadowT );
  892.   /*  /  */
  893.   make_hor_seg( shadow,
  894.         x, y + height -1,
  895.         x + width - shadowT - 1, y + mid, shadowT );
  896.  
  897.   triangle[0].x = x + 1;
  898.   triangle[0].y = y + height - 1;
  899.   triangle[1].x = x + width - 1;
  900.   triangle[1].y = y + mid;
  901.   triangle[2].x = x + 1;
  902.   triangle[2].y = y;
  903.  
  904.   XFillPolygon( dpy, win, bgGC, triangle, 3, Convex, ArcChord );
  905.  
  906.   XDrawSegments( dpy, win, shadowGC, shadow, shadowT );
  907.   XDrawSegments( dpy, win, shineGC, shine, shadowT * 2 );
  908. }
  909.  
  910. static void draw_dimple(Display *dpy, Drawable win, GC shine, GC shadow,
  911.             int x, int y, int width, int height)
  912. {
  913.   XDrawArc(dpy, win, shine, x, y, width, height, 46*64, 180*64);
  914.   XDrawArc(dpy, win, shadow, x, y, width, height, 45*64, -179*64);
  915. }
  916.  
  917. /*
  918. ** Scrollbar values -> pixels, pixels -> scrollbar values
  919. */
  920.  
  921. static void seg_pixel_sizes(XlwScrollBarWidget w, int *above_return, int *ss_return,
  922.                 int *below_return)
  923. {
  924.   float total, height, fuz;
  925.   int value;
  926.   int above, ss, below;
  927.  
  928.   height= widget_h(w);
  929.   if( w->sb.showArrows ) height -= (2*arrow_h(w));
  930.  
  931.   value = w->sb.value - w->sb.minimum;
  932.  
  933.   total = w->sb.maximum - w->sb.minimum;
  934.   fuz   = total / 2;
  935.  
  936.   ss    = ((height * w->sb.sliderSize + fuz) / total);
  937.   above = ((height * value + fuz) / total);
  938.   below = ((height) - (ss + above));
  939.  
  940.   /* Dont' let knob get smaller than SS_MIN */
  941.   if( ss < SS_MIN )
  942.       {
  943.       /* add a percent amount for interger rounding */
  944.       float tmp = ((((float)(SS_MIN - ss) * (float)value)) / total) + 0.5;
  945.  
  946.       above -= (int)tmp;
  947.       ss     = SS_MIN;
  948.       below = ((height) - (ss + above));
  949.  
  950.       if( above < 0 )
  951.       {
  952.       above = 0;
  953.       below = height - ss;
  954.       }
  955.       if( below < 0 )
  956.       {
  957.       above = height - ss;
  958.       below = 0;
  959.       }
  960.       if( ss > height )
  961.       {
  962.       above = 0;
  963.       ss    = height;
  964.       below = 0;
  965.       }
  966.       }
  967.  
  968.   *above_return = above;
  969.   *ss_return    = ss;
  970.   *below_return = below;
  971.  
  972.   CHECK(w);
  973. }
  974.  
  975. static void verify_values(XlwScrollBarWidget w)
  976. {
  977.   int total = w->sb.maximum - w->sb.minimum;
  978.  
  979.   if( w->sb.sliderSize > total )
  980.       {
  981.       w->sb.sliderSize = total;
  982.       }
  983.   if( w->sb.pageIncrement > total )
  984.       {
  985.       w->sb.pageIncrement = total;
  986.       }
  987.   if( w->sb.increment > total )
  988.       {
  989.       w->sb.increment = total;
  990.       }
  991.   if( w->sb.value < w->sb.minimum )
  992.       {
  993.       w->sb.value = w->sb.minimum;
  994.       }
  995.   if( w->sb.value > w->sb.maximum - w->sb.sliderSize)
  996.       {
  997.       w->sb.value = w->sb.maximum - w->sb.sliderSize;
  998.       }
  999. }
  1000.  
  1001. static int value_from_pixel(XlwScrollBarWidget w, int above)
  1002. {
  1003.   float total, height, fuz;
  1004.   int value, ss;
  1005.  
  1006.   height= widget_h(w);
  1007.   if( w->sb.showArrows ) height -= (2*arrow_h(w));
  1008.  
  1009.   total = w->sb.maximum - w->sb.minimum;
  1010.   fuz   = height / 2;
  1011.  
  1012.   ss    = ((height * w->sb.sliderSize + (total / 2)) / total);
  1013.  
  1014.   if( ss < SS_MIN )
  1015.       {
  1016.       /* add a percent amount for interger rounding */
  1017.       above += ((((SS_MIN - ss) * above) + fuz) / height);
  1018.       }
  1019.  
  1020.   {
  1021.     /* Prevent SIGFPE's that would occur if we don't truncate the
  1022.        value. */
  1023.     float floatval = w->sb.minimum + ((float)(above * total + fuz ) / height);
  1024.     if (floatval >= (float) INT_MAX)
  1025.       value = INT_MAX;
  1026.     else if (floatval <= (float) INT_MIN)
  1027.       value = INT_MIN;
  1028.     else
  1029.       value = floatval;
  1030.   }
  1031.  
  1032.   return( value );
  1033. }
  1034.  
  1035.  
  1036. static void redraw_dimple(XlwScrollBarWidget w, Display *dpy, Window win,
  1037.               int x, int y, int width, int height)
  1038. {
  1039.   GC shine, shadow;
  1040.   int shadowT, size;
  1041.  
  1042.   if( KNOB_DIMPLE == knob_style(w) )
  1043.       {
  1044.       if( w->sb.armed == ARM_KNOB )
  1045.       {
  1046.       shine = w->sb.bottomShadowGC;
  1047.       shadow = w->sb.topShadowGC;
  1048.       }
  1049.       else
  1050.       {
  1051.       shine = w->sb.topShadowGC;
  1052.       shadow = w->sb.bottomShadowGC;
  1053.       }
  1054.  
  1055.       shadowT = w->sb.shadowThickness;
  1056.  
  1057.       x += shadowT;
  1058.       y += shadowT;
  1059.       width  -= 2*shadowT;
  1060.       height -= 2*shadowT;
  1061.  
  1062.       size = (width < height ? width : height) * 3 / 4;
  1063.  
  1064.       if( size%2 != (width < height ? width : height)%2 ) size--;
  1065.  
  1066.       DBUG (fprintf(stderr, "%d %d\n",
  1067.             x + (width / 2) - (size / 2) - 2*shadowT,
  1068.             width - size - shadowT));
  1069.  
  1070.       draw_dimple( dpy, win, shine, shadow,
  1071.            x + (width / 2) - (size / 2),
  1072.            y + (height / 2) - (size / 2),
  1073.            size, size );
  1074.       }
  1075. }
  1076.  
  1077. static void draw_knob(XlwScrollBarWidget w, int above, int ss, int below)
  1078. {
  1079.   Display *dpy = XtDisplay((Widget)w);
  1080.   Window   win = XtWindow((Widget)w);
  1081.   int x, y, width, height;
  1082.   int shadowT;
  1083.  
  1084.   x       = widget_x(w);
  1085.   y       = widget_y(w);
  1086.   width   = widget_w(w);
  1087.   height  = widget_h(w);
  1088.  
  1089.   shadowT = w->sb.shadowThickness;
  1090.  
  1091.   if(shadowT > (width / 2)) shadowT = (width / 2);
  1092.   if(shadowT > (height / 2)) shadowT = (height / 2);
  1093.   if(shadowT <= 0) return;
  1094.  
  1095.   if( w->sb.showArrows && !arrow_same_end(w) ) y += (arrow_h(w));
  1096.  
  1097.   /* trough above knob */
  1098.   if( above > 0 )
  1099.       {
  1100.       if( VERT(w) )
  1101.       XClearArea( dpy, win, x, y, width, above, False );
  1102.       else
  1103.       XClearArea( dpy, win, y, x, above, width, False );
  1104.       }
  1105.  
  1106.   /* knob */
  1107.   if( VERT(w) )
  1108.       {
  1109.       draw_shadows( dpy, win, w->sb.topShadowGC, w->sb.bottomShadowGC,
  1110.             x, y + above, width, ss, shadowT);
  1111.       XFillRectangle( dpy, win,
  1112.               w->sb.backgroundGC,
  1113.               x+shadowT, y + above + shadowT, width-2*shadowT, ss-2*shadowT );
  1114.       redraw_dimple(w, dpy, win, x, y + above, width, ss);
  1115.       }
  1116.   else
  1117.       {
  1118.       draw_shadows( dpy, win, w->sb.topShadowGC, w->sb.bottomShadowGC,
  1119.             y + above, x, ss, width, shadowT);
  1120.       XFillRectangle( dpy, win,
  1121.               w->sb.backgroundGC,
  1122.               y + above + shadowT, x+shadowT, ss-2*shadowT, width-2*shadowT );
  1123.       redraw_dimple(w, dpy, win, y + above, x, ss, width);
  1124.       }
  1125.  
  1126.   /* trough below knob */
  1127.   if( below > 0 )
  1128.       {
  1129.       if( VERT(w) )
  1130.       XClearArea( dpy, win, x, y + above + ss, width, below, False );
  1131.       else
  1132.       XClearArea( dpy, win, y + above + ss, x, below, width, False );
  1133.       }
  1134.  
  1135.   CHECK(w);
  1136. }
  1137.  
  1138. static void redraw_up_arrow(XlwScrollBarWidget w, Boolean armed, Boolean clear_behind)
  1139. {
  1140.   Display *dpy = XtDisplay((Widget)w);
  1141.   Window   win = XtWindow((Widget)w);
  1142.   GC bg, shine, shadow;
  1143.   int x, y, width, height, arrow_height, shadowT;
  1144.  
  1145.   x       = widget_x(w);
  1146.   y       = widget_y(w);
  1147.   width   = widget_w(w);
  1148.   height  = widget_h(w);
  1149.   arrow_height = arrow_h(w);
  1150.  
  1151.   shadowT = w->sb.shadowThickness;
  1152.   bg      = w->sb.backgroundGC;
  1153.  
  1154.   if( armed )
  1155.       {
  1156.       shine   = w->sb.bottomShadowGC;
  1157.       shadow  = w->sb.topShadowGC;
  1158.       }
  1159.   else
  1160.       {
  1161.       shine   = w->sb.topShadowGC;
  1162.       shadow  = w->sb.bottomShadowGC;
  1163.       }
  1164.  
  1165.   if( VERT(w) )
  1166.       {
  1167.       if( arrow_same_end(w) )
  1168.       {
  1169.       y += height - 2*arrow_h(w) + 2;
  1170.       }
  1171.       if( clear_behind )
  1172.       XClearArea( dpy, win, x, y, width, arrow_height + 1, False );
  1173.       draw_arrow_up( dpy, win, bg, shine, shadow,
  1174.              x + (width - arrow_height)/2, y,
  1175.              arrow_height, arrow_height, shadowT );
  1176.       }
  1177.   else
  1178.       {
  1179.       if( arrow_same_end(w) )
  1180.       {
  1181.       y += height - 2*arrow_h(w);
  1182.       }
  1183.       if( clear_behind )
  1184.       XClearArea( dpy, win, y, x, arrow_height + 1, height, False );
  1185.       draw_arrow_left( dpy, win, bg, shine, shadow,
  1186.                y, x + (width - arrow_height)/2,
  1187.                arrow_height, arrow_height, shadowT );
  1188.       }
  1189. }
  1190.  
  1191. static void redraw_down_arrow(XlwScrollBarWidget w, Boolean armed, Boolean clear_behind)
  1192. {
  1193.   Display *dpy = XtDisplay((Widget)w);
  1194.   Window   win = XtWindow((Widget)w);
  1195.   GC bg, shine, shadow;
  1196.   int x, y, width, height, arrow_height, shadowT;
  1197.  
  1198.   x       = widget_x(w);
  1199.   y       = widget_y(w);
  1200.   width   = widget_w(w);
  1201.   height  = widget_h(w);
  1202.   arrow_height = arrow_h(w);
  1203.  
  1204.   shadowT = w->sb.shadowThickness;
  1205.   bg      = w->sb.backgroundGC;
  1206.  
  1207.   if( armed )
  1208.       {
  1209.       shine   = w->sb.bottomShadowGC;
  1210.       shadow  = w->sb.topShadowGC;
  1211.       }
  1212.   else
  1213.       {
  1214.       shine   = w->sb.topShadowGC;
  1215.       shadow  = w->sb.bottomShadowGC;
  1216.       }
  1217.  
  1218.   if( VERT(w) )
  1219.       {
  1220.       if( clear_behind )
  1221.       XClearArea( dpy, win, x, y + height - arrow_height, width, arrow_height + 1, False );
  1222.       draw_arrow_down( dpy, win, bg, shine, shadow,
  1223.                x + (width - arrow_height)/2, y + height - arrow_height + 1,
  1224.                arrow_height, arrow_height, shadowT );
  1225.       }
  1226.   else
  1227.       {
  1228.       if( clear_behind )
  1229.       XClearArea( dpy, win, y + height - arrow_height, x, arrow_height + 1, height, False );
  1230.       draw_arrow_right( dpy, win, bg, shine, shadow,
  1231.             y + height - arrow_height + 1, x + (width - arrow_height)/2,
  1232.             arrow_height, arrow_height, shadowT );
  1233.       }
  1234. }
  1235.  
  1236. static void redraw_everything(XlwScrollBarWidget w, Region region, Boolean behind_arrows)
  1237. {
  1238.   Display *dpy = XtDisplay((Widget)w);
  1239.   Window   win = XtWindow((Widget)w);
  1240.   int x, y, width, height, shadowT, tmp;
  1241.  
  1242.   x       = widget_x(w);
  1243.   y       = widget_y(w);
  1244.   width   = widget_w(w);
  1245.   height  = widget_h(w);
  1246.   shadowT = w->sb.shadowThickness;
  1247.  
  1248.   if( w->sb.showArrows )
  1249.       {
  1250.       if( region == NULL || XRectInRegion( region, x, y, width, width ) )
  1251.       {
  1252.       redraw_up_arrow( w, False, behind_arrows );
  1253.       }
  1254.       if( VERT(w) )
  1255.       {
  1256.       y = y + height - width + 1;
  1257.       }
  1258.       else
  1259.       {
  1260.       tmp = y;
  1261.       y = x;
  1262.       x = tmp + height - width + 1;
  1263.       }
  1264.       if( region == NULL || XRectInRegion( region, x, y, width, width ) )
  1265.       {
  1266.       redraw_down_arrow( w, False, behind_arrows );
  1267.       }
  1268.       }
  1269.  
  1270.   draw_shadows( dpy, win, w->sb.bottomShadowGC, w->sb.topShadowGC,
  1271.         0, 0, w->core.width, w->core.height, shadowT);
  1272.  
  1273.   draw_knob( w, w->sb.above, w->sb.ss, w->sb.below );
  1274.  
  1275. }
  1276.  
  1277. /************************************************************************
  1278. **
  1279. ** Method funcitons
  1280. **
  1281. */
  1282.  
  1283. /*
  1284. ** Initialize
  1285. */
  1286. static void Initialize(Widget treq, Widget tnew, ArgList args, Cardinal *num_args)
  1287. {
  1288.   XlwScrollBarWidget request = (XlwScrollBarWidget) treq;
  1289.   XlwScrollBarWidget w = (XlwScrollBarWidget) tnew;
  1290.   Display *dpy = XtDisplay((Widget)w);
  1291.   Window win = RootWindowOfScreen( DefaultScreenOfDisplay(dpy) );
  1292.  
  1293.   DBUG(fprintf(stderr, "Initialize\n"));
  1294.  
  1295.   if( request->core.width == 0 ) w->core.width += (VERT(w) ? 12 : 25);
  1296.   if( request->core.height == 0 ) w->core.height += (VERT(w) ? 25 : 12);
  1297.  
  1298.   verify_values(w);
  1299.  
  1300.   w->sb.lastY = 0;
  1301.   w->sb.above = 0;
  1302.   w->sb.ss    = 0;
  1303.   w->sb.below = 0;
  1304.   w->sb.armed = ARM_NONE;
  1305.  
  1306.   if( w->sb.shadowThickness > 5 ) w->sb.shadowThickness = 5;
  1307.  
  1308.   w->sb.grayPixmap =
  1309.       XCreatePixmapFromBitmapData( dpy, win, (char *) gray_bits, gray_width,
  1310.                    gray_height, 1, 0, 1);
  1311.  
  1312.   make_trough_pixel( w );
  1313.  
  1314.   make_shadow_pixels( w );
  1315.  
  1316.   w->sb.backgroundGC   = get_gc(w, w->core.background_pixel, w->core.background_pixel, None);
  1317.   w->sb.topShadowGC    = get_gc(w, w->sb.topShadowColor, w->core.background_pixel, w->sb.topShadowPixmap);
  1318.   w->sb.bottomShadowGC = get_gc(w, w->sb.bottomShadowColor, w->core.background_pixel, w->sb.bottomShadowPixmap);
  1319.  
  1320.   w->sb.fullRedrawNext = True;
  1321. }
  1322.  
  1323. /*
  1324. ** Destroy
  1325. */
  1326. static void Destroy(Widget widget)
  1327. {
  1328.   XlwScrollBarWidget w = (XlwScrollBarWidget) widget;
  1329.   Display *dpy = XtDisplay((Widget)w);
  1330.  
  1331.   DBUG(fprintf(stderr, "Destroy\n"));
  1332.  
  1333.   XtReleaseGC(widget, w->sb.bottomShadowGC);
  1334.   XtReleaseGC(widget, w->sb.topShadowGC);
  1335.   XtReleaseGC(widget, w->sb.backgroundGC);
  1336.  
  1337.   XFreePixmap( dpy, w->sb.grayPixmap );
  1338. }
  1339.  
  1340. /*
  1341. ** Realize
  1342. */
  1343. static void Realize(Widget widget, XtValueMask *valuemask, XSetWindowAttributes *attr)
  1344. {
  1345.   XlwScrollBarWidget w = (XlwScrollBarWidget) widget;
  1346.   Display *dpy = XtDisplay((Widget)w);
  1347.   Window win;
  1348.   XSetWindowAttributes win_attr;
  1349.  
  1350.   DBUG(fprintf(stderr, "Realize\n"));
  1351.  
  1352.   (*coreClassRec.core_class.realize)(widget, valuemask, attr);
  1353.  
  1354.   win = XtWindow((Widget)w);
  1355.  
  1356.   seg_pixel_sizes(w, &w->sb.above, &w->sb.ss, &w->sb.below);
  1357.  
  1358.   XSetWindowBackground( dpy, win, w->sb.troughColor);
  1359.  
  1360.   /* Change bit gravity so widget is not cleared on resize */
  1361.   win_attr.bit_gravity   = NorthWestGravity;
  1362.   XChangeWindowAttributes( dpy, win, CWBitGravity , &win_attr);
  1363.  
  1364. }
  1365.  
  1366. /*
  1367. ** Resize
  1368. */
  1369. static void Resize(Widget widget)
  1370. {
  1371.   XlwScrollBarWidget w = (XlwScrollBarWidget) widget;
  1372.   Display *dpy = XtDisplay((Widget)w);
  1373.   Window win = XtWindow((Widget)w);
  1374.  
  1375.   if( XtIsRealized(widget) )
  1376.       {
  1377.       DBUG(fprintf(stderr, "Resize = %08lx\n", w));
  1378.  
  1379.       seg_pixel_sizes(w, &w->sb.above, &w->sb.ss, &w->sb.below);
  1380.  
  1381.       /*redraw_everything(w, NULL, True);*/
  1382.  
  1383.       w->sb.fullRedrawNext = True;
  1384.       /* Force expose event */
  1385.       XClearArea(dpy, win, widget_x(w), widget_y(w), 1, 1, True);
  1386.       }
  1387. }
  1388.  
  1389. /*
  1390. ** Redisplay
  1391. */
  1392. static void Redisplay(Widget widget, XEvent *event, Region region)
  1393. {
  1394.   XlwScrollBarWidget w = (XlwScrollBarWidget) widget;
  1395.  
  1396.   DBUG(fprintf(stderr, "Redisplay = %08lx\n", w));
  1397.  
  1398.   if( XtIsRealized(widget) )
  1399.       {
  1400.       if( w->sb.fullRedrawNext )
  1401.       {
  1402.       redraw_everything(w, NULL, True);
  1403.       }
  1404.       else
  1405.       {
  1406.       redraw_everything(w, region, False);
  1407.       }
  1408.       w->sb.fullRedrawNext = False;
  1409.       }
  1410. }
  1411.  
  1412. /*
  1413. ** SetValues
  1414. */
  1415. static Boolean SetValues(Widget current, Widget request, Widget neww, ArgList args, Cardinal *num_args)
  1416. {
  1417.   XlwScrollBarWidget cur = (XlwScrollBarWidget) current;
  1418.   XlwScrollBarWidget w = (XlwScrollBarWidget) neww;
  1419.   Boolean do_redisplay = False;
  1420.  
  1421.   if( cur->sb.troughColor != w->sb.troughColor )
  1422.       {
  1423.       if( XtIsRealized((Widget)w) )
  1424.       {
  1425.       XSetWindowBackground( XtDisplay((Widget)w), XtWindow((Widget)w),
  1426.                 w->sb.troughColor);
  1427.       do_redisplay = True;
  1428.       }
  1429.       }
  1430.  
  1431.   if( cur->core.background_pixel != w->core.background_pixel )
  1432.       {
  1433.       XtReleaseGC((Widget)cur, cur->sb.backgroundGC);
  1434.       w->sb.backgroundGC = get_gc(w, w->core.background_pixel, w->core.background_pixel, None);
  1435.       do_redisplay = True;
  1436.       }
  1437.  
  1438.   if( cur->sb.topShadowColor != w->sb.topShadowColor ||
  1439.       cur->sb.topShadowPixmap != w->sb.topShadowPixmap )
  1440.       {
  1441.       XtReleaseGC((Widget)cur, cur->sb.topShadowGC);
  1442.       w->sb.topShadowGC = get_gc(w, w->sb.topShadowColor, w->core.background_pixel, w->sb.topShadowPixmap);
  1443.       do_redisplay = True;
  1444.       }
  1445.  
  1446.   if( cur->sb.bottomShadowColor != w->sb.bottomShadowColor ||
  1447.       cur->sb.bottomShadowPixmap != w->sb.bottomShadowPixmap )
  1448.       {
  1449.       XtReleaseGC((Widget)cur, cur->sb.bottomShadowGC);
  1450.       w->sb.bottomShadowGC = get_gc(w, w->sb.bottomShadowColor, w->core.background_pixel, w->sb.bottomShadowPixmap);
  1451.       do_redisplay = True;
  1452.       }
  1453.  
  1454.   if( cur->sb.orientation != w->sb.orientation )
  1455.       {
  1456.       do_redisplay = True;
  1457.       }
  1458.  
  1459.  
  1460.   if( cur->sb.minimum       != w->sb.minimum       ||
  1461.       cur->sb.maximum       != w->sb.maximum       ||
  1462.       cur->sb.sliderSize    != w->sb.sliderSize    ||
  1463.       cur->sb.value         != w->sb.value         ||
  1464.       cur->sb.pageIncrement != w->sb.pageIncrement ||
  1465.       cur->sb.increment     != w->sb.increment )
  1466.       {
  1467.       verify_values(w);
  1468.       if( XtIsRealized((Widget)w) )
  1469.       {
  1470.       seg_pixel_sizes(w, &w->sb.above, &w->sb.ss, &w->sb.below);
  1471.       draw_knob( w, w->sb.above, w->sb.ss, w->sb.below );
  1472.       }
  1473.       }
  1474.  
  1475.   if( w->sb.shadowThickness > 5 ) w->sb.shadowThickness = 5;
  1476.  
  1477.   return( do_redisplay );
  1478. }
  1479.  
  1480. void XlwScrollBarGetValues(Widget widget, int *value, int *sliderSize,
  1481.                int *increment, int *pageIncrement)
  1482. {
  1483.   XlwScrollBarWidget w = (XlwScrollBarWidget)widget;
  1484.  
  1485.   if( w && XtClass((Widget)w) == xlwScrollBarWidgetClass )
  1486.       {
  1487.       if( value )         *value         = w->sb.value;
  1488.       if( sliderSize )    *sliderSize    = w->sb.sliderSize;
  1489.       if( increment )     *increment     = w->sb.increment;
  1490.       if( pageIncrement ) *pageIncrement = w->sb.pageIncrement;
  1491.       }
  1492. }
  1493.  
  1494. void XlwScrollBarSetValues(Widget widget, int value, int sliderSize,
  1495.                int increment, int pageIncrement, Boolean notify)
  1496. {
  1497.   XlwScrollBarWidget w = (XlwScrollBarWidget)widget;
  1498.   int last_value;
  1499.  
  1500.   if( w && XtClass((Widget)w) == xlwScrollBarWidgetClass &&
  1501.       (w->sb.value         != value         ||
  1502.        w->sb.sliderSize    != sliderSize    ||
  1503.        w->sb.increment     != increment     ||
  1504.        w->sb.pageIncrement != pageIncrement ))
  1505.       {
  1506.       w->sb.value         = value;
  1507.       w->sb.sliderSize    = sliderSize;
  1508.       w->sb.increment     = increment;
  1509.       w->sb.pageIncrement = pageIncrement;
  1510.  
  1511.       verify_values(w);
  1512.  
  1513.       if( XtIsRealized(widget) )
  1514.       {
  1515.       seg_pixel_sizes(w, &w->sb.above, &w->sb.ss, &w->sb.below);
  1516.       draw_knob(w, w->sb.above, w->sb.ss, w->sb.below);
  1517.  
  1518.       last_value  = w->sb.value;
  1519.       w->sb.value = value_from_pixel(w, w->sb.above);
  1520.       verify_values(w);
  1521.  
  1522.       if( w->sb.value != last_value && notify )
  1523.           {
  1524.           call_callbacks( w, XmCR_VALUE_CHANGED, w->sb.value, 0, NULL );
  1525.           }
  1526.       }
  1527.       }
  1528. }
  1529.  
  1530. /************************************************************************
  1531. **
  1532. ** Action funcitons
  1533. **
  1534. */
  1535.  
  1536. static void timer(XtPointer data, XtIntervalId *id)
  1537. {
  1538.   XlwScrollBarWidget w = (XlwScrollBarWidget)data;
  1539.   int reason, last_value;
  1540.  
  1541.   if( w->sb.armed != ARM_NONE )
  1542.       {
  1543.       last_value = w->sb.value;
  1544.       reason     = XmCR_NONE;
  1545.  
  1546.       switch( w->sb.armed )
  1547.       {
  1548.       case ARM_PAGEUP:
  1549.           w->sb.value = safe_subtract( w->sb.value, w->sb.pageIncrement );
  1550.           reason = XmCR_PAGE_DECREMENT;
  1551.           break;
  1552.       case ARM_PAGEDOWN:
  1553.           w->sb.value = safe_add( w->sb.value, w->sb.pageIncrement );
  1554.           reason = XmCR_PAGE_INCREMENT;
  1555.           break;
  1556.       case ARM_UP:
  1557.           w->sb.value = safe_subtract( w->sb.value, w->sb.increment );
  1558.           reason = XmCR_DECREMENT;
  1559.           break;
  1560.       case ARM_DOWN:
  1561.           w->sb.value = safe_add( w->sb.value, w->sb.increment );
  1562.           reason = XmCR_INCREMENT;
  1563.           break;
  1564.       }
  1565.  
  1566.       verify_values(w);
  1567.  
  1568.       if( last_value != w->sb.value )
  1569.       {
  1570.       seg_pixel_sizes(w, &w->sb.above, &w->sb.ss, &w->sb.below);
  1571.       draw_knob(w, w->sb.above, w->sb.ss, w->sb.below);
  1572.       
  1573.       call_callbacks( w, reason, w->sb.value, 0, NULL );
  1574.  
  1575.       XtAppAddTimeOut( XtWidgetToApplicationContext((Widget)w),
  1576.                (unsigned long) w->sb.repeatDelay,
  1577.                timer,  (XtPointer) w );
  1578.       }
  1579.       }
  1580. }
  1581.  
  1582. static int what_button(XlwScrollBarWidget w, int mouse_x, int mouse_y)
  1583. {
  1584.   int x, y, width, height, arrow_height_top, arrow_height_bottom;
  1585.   int where;
  1586.  
  1587.   x       = widget_x(w);
  1588.   y       = widget_y(w);
  1589.   width   = widget_w(w);
  1590.   height  = widget_h(w);
  1591.  
  1592. #if 0
  1593.   arrow_height = w->sb.showArrows ? arrow_h(w) : 0;
  1594. #endif
  1595.   if( w->sb.showArrows )
  1596.       {
  1597.       if( arrow_same_end(w) )
  1598.       {
  1599.       arrow_height_top = 0;
  1600.       arrow_height_bottom = 2*arrow_h(w);
  1601.       }
  1602.       else
  1603.       {
  1604.       arrow_height_top = arrow_height_bottom = arrow_h(w);
  1605.       }
  1606.       }
  1607.   else
  1608.       {
  1609.       arrow_height_top = arrow_height_bottom = 0;
  1610.       }
  1611.  
  1612.   where = BUTTON_NONE;
  1613.  
  1614.   if( mouse_x > x && mouse_x < (x + width) )
  1615.       {
  1616.       if( mouse_y > (y + arrow_height_top) && mouse_y < (y + height - arrow_height_bottom) )
  1617.       {
  1618.       if( mouse_y < (y + w->sb.above + arrow_height_top) )
  1619.           {
  1620.           where = BUTTON_TROUGH_ABOVE;
  1621.           }
  1622.       else if( mouse_y > (y + w->sb.above + w->sb.ss + arrow_height_top) )
  1623.           {
  1624.           where = BUTTON_TROUGH_BELOW;
  1625.           }
  1626.       else
  1627.           {
  1628.           where = BUTTON_KNOB;
  1629.           }
  1630.       }
  1631.       else if( arrow_same_end(w) )
  1632.       {
  1633.       if( mouse_y > (y + height - arrow_height_bottom + 1) && mouse_y < (y + height) )
  1634.           {
  1635.           if( mouse_y < (y + height - arrow_height_bottom/2) )
  1636.           {
  1637.           where = BUTTON_UP_ARROW;
  1638.           }
  1639.           else
  1640.           {
  1641.           where = BUTTON_DOWN_ARROW;
  1642.           }
  1643.           }
  1644.       }
  1645.       else
  1646.       {
  1647.       if( mouse_y > y && mouse_y < (y + arrow_height_top) )
  1648.           {
  1649.           where = BUTTON_UP_ARROW;
  1650.           }
  1651.       else if( mouse_y > (y + height - arrow_height_bottom + 1) && mouse_y < (y + height) )
  1652.           {
  1653.           where = BUTTON_DOWN_ARROW;
  1654.           }
  1655.       }
  1656.       }
  1657. #if 0
  1658.   if( mouse_x > x && mouse_x < (x + width) )
  1659.       {
  1660.       if( mouse_y > (y + arrow_height) && mouse_y < (y + height - arrow_height) )
  1661.       {
  1662.       if( mouse_y < (y+w->sb.above+arrow_height) )
  1663.           {
  1664.           where = BUTTON_TROUGH_ABOVE;
  1665.           }
  1666.       else if( mouse_y > (y + w->sb.above + w->sb.ss + arrow_height) )
  1667.           {
  1668.           where = BUTTON_TROUGH_BELOW;
  1669.           }
  1670.       else
  1671.           {
  1672.           where = BUTTON_KNOB;
  1673.           }
  1674.       }
  1675.       else if( mouse_y > y && mouse_y < (y + arrow_height) )
  1676.       {
  1677.       where = BUTTON_UP_ARROW;
  1678.       }
  1679.       else if( mouse_y > (y + height - arrow_height + 1) && mouse_y < (y + height) )
  1680.       {
  1681.       where = BUTTON_DOWN_ARROW;
  1682.       }
  1683.       }
  1684. #endif
  1685.   return( where );
  1686. }
  1687.  
  1688. /*
  1689. ** Select
  1690. */
  1691. static void Select(Widget widget, XEvent *event, String *parms, Cardinal *num_parms)
  1692. {
  1693.   XlwScrollBarWidget w = (XlwScrollBarWidget)widget;
  1694.   int mouse_x, mouse_y;
  1695.   int reason, last_value;
  1696.  
  1697.   DBUG(fprintf(stderr, "Select:\n"));
  1698.  
  1699.   mouse_x = event_x( w, event );
  1700.   mouse_y = event_y( w, event );
  1701.  
  1702.   w->sb.savedValue = w->sb.value;
  1703.  
  1704.   last_value = w->sb.value;
  1705.   reason     = XmCR_NONE;
  1706.  
  1707.   XtGrabKeyboard( (Widget)w, False, GrabModeAsync, GrabModeAsync, event->xbutton.time );
  1708.  
  1709.   switch( what_button(w, mouse_x, mouse_y) )
  1710.       {
  1711.       case BUTTON_TROUGH_ABOVE:
  1712.       w->sb.value = safe_subtract( w->sb.value, w->sb.pageIncrement );
  1713.       w->sb.armed = ARM_PAGEUP;
  1714.       reason      = XmCR_PAGE_DECREMENT;
  1715.       break;
  1716.       case BUTTON_TROUGH_BELOW:
  1717.       w->sb.value = safe_add( w->sb.value, w->sb.pageIncrement );
  1718.       w->sb.armed = ARM_PAGEDOWN;
  1719.       reason      = XmCR_PAGE_INCREMENT;
  1720.       break;
  1721.       case BUTTON_KNOB:
  1722.       w->sb.lastY = mouse_y;
  1723.       w->sb.armed = ARM_KNOB;
  1724.       draw_knob(w, w->sb.above, w->sb.ss, w->sb.below);
  1725.       break;
  1726.       case BUTTON_UP_ARROW:
  1727.       if( event->xbutton.state & ControlMask )
  1728.           {
  1729.           w->sb.value = INT_MIN;
  1730.           w->sb.armed = ARM_UP;
  1731.           reason      = XmCR_TO_TOP;
  1732.           }
  1733.       else
  1734.           {
  1735.           w->sb.value = safe_subtract( w->sb.value, w->sb.increment );
  1736.           w->sb.armed = ARM_UP;
  1737.           reason      = XmCR_DECREMENT;
  1738.           }
  1739.       redraw_up_arrow(w, True, False);
  1740.       break;
  1741.       case BUTTON_DOWN_ARROW:
  1742.       if( event->xbutton.state & ControlMask )
  1743.           {
  1744.           w->sb.value = INT_MAX;
  1745.           w->sb.armed = ARM_DOWN;
  1746.           reason      = XmCR_TO_BOTTOM;
  1747.           }
  1748.       else
  1749.           {
  1750.           w->sb.value = safe_add( w->sb.value, w->sb.increment );
  1751.           w->sb.armed = ARM_DOWN;
  1752.           reason      = XmCR_INCREMENT;
  1753.           }
  1754.       redraw_down_arrow(w, True, False);
  1755.       break;
  1756.       }
  1757.  
  1758.   verify_values(w);
  1759.  
  1760.   if( last_value != w->sb.value )
  1761.       {
  1762.       seg_pixel_sizes(w, &w->sb.above, &w->sb.ss, &w->sb.below);
  1763.       draw_knob(w, w->sb.above, w->sb.ss, w->sb.below);
  1764.       
  1765.       call_callbacks( w, reason, w->sb.value, mouse_y, event );
  1766.  
  1767.       XtAppAddTimeOut( XtWidgetToApplicationContext((Widget)w),
  1768.                (unsigned long) w->sb.initialDelay,
  1769.                timer,  (XtPointer) w );
  1770.       }
  1771.  
  1772.   CHECK(w);
  1773. }
  1774.  
  1775. /*
  1776. ** Drag
  1777. */
  1778. static void Drag(Widget widget, XEvent *event, String *parms, Cardinal *num_parms)
  1779. {
  1780.   XlwScrollBarWidget w = (XlwScrollBarWidget)widget;
  1781.   int diff;
  1782.   int height, mouse_y;
  1783.   int last_value, last_above;
  1784.  
  1785.   DBUG(fprintf(stderr, "Drag:\n"));
  1786.  
  1787.   if( w->sb.armed == ARM_KNOB )
  1788.       {
  1789.       height  = widget_h(w);
  1790.       if( w->sb.showArrows ) height -= (2*arrow_h(w));
  1791.  
  1792.       mouse_y = event_y( w, event );
  1793.  
  1794.       diff = mouse_y - w->sb.lastY;
  1795.  
  1796.       last_above = w->sb.above;
  1797.       last_value = w->sb.value;
  1798.  
  1799.       if( diff < 0 )
  1800.       {
  1801.       /* up */
  1802.       w->sb.above -= (-diff);
  1803.       if( w->sb.above < 0 )
  1804.           {
  1805.           mouse_y = (mouse_y - w->sb.above);
  1806.           w->sb.above = 0;
  1807.           diff = 0;
  1808.           w->sb.below = height - w->sb.ss;
  1809.           }
  1810.       w->sb.below -= diff;
  1811.       CHECK(w);
  1812.       }
  1813.       else if( diff > 0 )
  1814.       {
  1815.       /* down */
  1816.       w->sb.above += diff;
  1817.       if( w->sb.above + w->sb.ss > height )
  1818.           {
  1819.           mouse_y = height + (mouse_y - (w->sb.above + w->sb.ss));
  1820.           w->sb.above = height - w->sb.ss;
  1821.           diff = 0;
  1822.           w->sb.below = 0;
  1823.           }
  1824.       w->sb.below -= diff;
  1825.       CHECK(w);
  1826.       }
  1827.  
  1828.       if( last_above != w->sb.above )
  1829.       {
  1830.       draw_knob(w, w->sb.above, w->sb.ss, w->sb.below);
  1831.  
  1832.       w->sb.lastY = mouse_y;
  1833.  
  1834.       w->sb.value = value_from_pixel(w, w->sb.above);
  1835.       verify_values(w);
  1836.       CHECK(w);
  1837.  
  1838.       if( w->sb.value != last_value )
  1839.           {
  1840.           call_callbacks( w, XmCR_DRAG, w->sb.value, event_y(w, event), event );
  1841.           }
  1842.       }
  1843.       }
  1844.   CHECK(w);
  1845. }
  1846.  
  1847. /*
  1848. ** Release
  1849. */
  1850. static void Release(Widget widget, XEvent *event, String *parms, Cardinal *num_parms)
  1851. {
  1852.   XlwScrollBarWidget w = (XlwScrollBarWidget)widget;
  1853.  
  1854.   DBUG(fprintf(stderr, "EndDrag:\n"));
  1855.  
  1856.   switch( w->sb.armed )
  1857.       {
  1858.       case ARM_KNOB:
  1859.       call_callbacks( w, XmCR_VALUE_CHANGED, w->sb.value, event_y(w, event), event );
  1860.       w->sb.armed = ARM_NONE;
  1861.       draw_knob(w, w->sb.above, w->sb.ss, w->sb.below);
  1862.       break;
  1863.       case ARM_UP:
  1864.       redraw_up_arrow(w, False, False);
  1865.       break;
  1866.       case ARM_DOWN:
  1867.       redraw_down_arrow(w, False, False);
  1868.       break;
  1869.       }
  1870.  
  1871.   XtUngrabKeyboard( (Widget)w, event->xbutton.time );
  1872.  
  1873.   w->sb.armed = ARM_NONE;
  1874. }
  1875.  
  1876. /*
  1877. ** Jump
  1878. */
  1879. static void Jump(Widget widget, XEvent *event, String *parms, Cardinal *num_parms)
  1880. {
  1881.   XlwScrollBarWidget w = (XlwScrollBarWidget)widget;
  1882.   int x, y, width, height, mouse_x, mouse_y;
  1883.   int arrow_height;
  1884.   int last_above, last_value;
  1885.  
  1886.   DBUG(fprintf(stderr, "Jump:\n"));
  1887.  
  1888.   x       = widget_x(w);
  1889.   y       = widget_y(w);
  1890.   width   = widget_w(w);
  1891.   height  = widget_h(w);
  1892.   mouse_x = event_x( w, event );
  1893.   mouse_y = event_y( w, event );
  1894.  
  1895.   arrow_height = w->sb.showArrows ? arrow_h(w) : 0;
  1896.  
  1897.   XtGrabKeyboard( (Widget)w, False, GrabModeAsync, GrabModeAsync, event->xbutton.time );
  1898.  
  1899.   switch( what_button(w, mouse_x, mouse_y) )
  1900.       {
  1901.       case BUTTON_TROUGH_ABOVE:
  1902.       case BUTTON_TROUGH_BELOW:
  1903.       case BUTTON_KNOB:
  1904.       w->sb.savedValue = w->sb.value;
  1905.  
  1906.       height -= (2*arrow_height);
  1907.       y      += arrow_height;
  1908.  
  1909.       last_above = w->sb.above;
  1910.       last_value = w->sb.value;
  1911.  
  1912.       w->sb.armed = ARM_KNOB;
  1913.       draw_knob(w, w->sb.above, w->sb.ss, w->sb.below);
  1914.  
  1915.       w->sb.above = mouse_y - (w->sb.ss / 2) - arrow_height;
  1916.       if( w->sb.above < 0 )
  1917.           {
  1918.           w->sb.above = 0;
  1919.           }
  1920.       else if( w->sb.above + w->sb.ss > height )
  1921.           {
  1922.           w->sb.above = height - w->sb.ss;
  1923.           }
  1924.       w->sb.below = (height - (w->sb.ss + w->sb.above));
  1925.  
  1926.       if( last_above != w->sb.above )
  1927.           {
  1928.           draw_knob(w, w->sb.above, w->sb.ss, w->sb.below);
  1929.  
  1930.           w->sb.value = value_from_pixel(w, w->sb.above);
  1931.           verify_values(w);
  1932.           CHECK(w);
  1933.  
  1934.           w->sb.lastY = mouse_y;
  1935.           w->sb.lastY = w->sb.above + arrow_height + (w->sb.ss / 2);
  1936.  
  1937.           if( w->sb.value != last_value )
  1938.           {
  1939.           call_callbacks( w, XmCR_DRAG, w->sb.value, event_y(w, event), event );
  1940.           }
  1941.           }
  1942.       break;
  1943.       }
  1944.   CHECK(w);
  1945. }
  1946.  
  1947. /*
  1948. ** Abort
  1949. */
  1950. static void Abort(Widget widget, XEvent *event, String *parms, Cardinal *num_parms)
  1951. {
  1952.   XlwScrollBarWidget w = (XlwScrollBarWidget)widget;
  1953.  
  1954.   DBUG(fprintf(stderr, "Abort:\n"));
  1955.  
  1956.   if( w->sb.armed != ARM_NONE )
  1957.       {
  1958.       if( w->sb.value != w->sb.savedValue )
  1959.       {
  1960.       w->sb.value = w->sb.savedValue;
  1961.  
  1962.       seg_pixel_sizes(w, &w->sb.above, &w->sb.ss, &w->sb.below);
  1963.       draw_knob(w, w->sb.above, w->sb.ss, w->sb.below);
  1964.  
  1965.       call_callbacks( w, XmCR_VALUE_CHANGED, w->sb.value, event_y(w, event), event );
  1966.       }
  1967.  
  1968.       switch( w->sb.armed )
  1969.       {
  1970.       case ARM_UP:
  1971.           redraw_up_arrow(w, False, False);
  1972.           break;
  1973.       case ARM_DOWN:
  1974.           redraw_down_arrow(w, False, False);
  1975.           break;
  1976.       }
  1977.  
  1978.       w->sb.armed = ARM_NONE;
  1979.  
  1980.       XtUngrabKeyboard( (Widget)w, event->xbutton.time );
  1981.       }
  1982. }
  1983.